package com.worldtolearn.user.bu;

import java.io.Serializable;

import javax.inject.Inject;
import javax.inject.Named;

import com.worldtolearn.logging.SingletonLogger;
import com.worldtolearn.user.dao.UserDAO;
import com.worldtolearn.user.entity.User;

@Named
public class UserBu implements IUserBu, Serializable {
//public class UserBu {

	private static final long serialVersionUID = 1L;
	
	@Inject
	UserDAO userDao; 
	
	private boolean connected = false;
	
	@Inject
	User user;
	

	public UserBu() {
		super();
		connected = false;
	}

	@Override
	public void create(User user) {
		userDao.create(user);
		SingletonLogger.monLogger.info("The user has been successfully created!");
	}

	@Override
	public User read(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(User user) {
		// TODO A décommenter
		userDao.update(user);
		SingletonLogger.monLogger.info("The user has been successfully updated!");
	}

	@Override
	public void delete(User user) {
		// TODO A décommenter
		userDao.delete(user);
		SingletonLogger.monLogger.info("The user has been successfully deleted!");
	}

	@Override
	public User getUserByMail(String mail) {
		return userDao.getUserByMail(mail);
	}

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	
	public boolean isConnected() {
		return connected;
	}

	public void setConnected(boolean connected) {
		this.connected = connected;
	}

	@Override
	public void connect(String mail, String password) {
		user = userDao.getUserByMail(mail);
		System.out.println("return from dao getUsr-erByEmail : " +user);
		if(user != null)
			if(user.getPassword().equals(password))
				connected = true;
	}

	@Override
	public void disconnect() {
		connected = false;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	
//	@Override
//	public boolean isValidUser(String mail, String password) {
//		boolean valid = false;
//		User user = userDao.getUserByMail(mail);
//		if(user != null)
//			if(user.getPassword().equals(password))
//				valid = true;
//		return valid;
//	}

}
